package com.etime.shycourse.controller.fileupload;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/file")
@CrossOrigin
@Api(tags = "文件上传")
public class FileUploadController {
    @Autowired
    private FastDFSClientWrapper dfsclient;

    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation("文件上传")
    public Map<String, String> upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 省略业务逻辑代码。。。
        String imgUrl = dfsclient.uploadFile(file);
        Map<String, String> map = new HashMap<>();
        map.put("url", imgUrl);
        map.put("code", "1");
        //System.out.println(imgUrl);
        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/uploadthumb", method = RequestMethod.POST)
    @ApiOperation("文件上传并生成缩微图")
    public Map<String,String> uploadAndCreateThumb(@RequestParam(value = "file", required = false) MultipartFile file,
                                                   HttpServletRequest request,
                                                   HttpServletResponse response) throws IOException {
        Map<String,String> map = dfsclient.uploadFileAndCreateThumb(file);
        System.out.println(map);
        return map;
    }

    /**
     * 上传文件到文件系统
     * @param file
     * @param request
     * @param response
     * @return
     */
    public Map<String,String> uploadFileSystem(@RequestParam(value = "file", required = false) MultipartFile file,
                                               HttpServletRequest request,
                                               HttpServletResponse response){

        Map<String,String> map = new HashMap<>();

        return map;
    }
}
